//
//  SHOAuthProgressView.swift
//  SHWeibo
//
//  Created by jingming wang on 16/7/24.
//  Copyright © 2016年 cpy.name. All rights reserved.
//

import UIKit

class SHOAuthProgressView: UIProgressView {
    
    // 定义一个定时器
    private weak var timer: NSTimer?
    
    private var isAnimating: Bool = true
    
    override init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: 64, width: K_SCREEN_WIDTH, height: 2))
        self.tintColor = UIColor.greenColor()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: -  供外界调用的方法
    // 开始动画
    func startAnimating(){
        self.progress = 0
        timer = NSTimer.scheduledTimerWithTimeInterval(1/60, target: self, selector: "valueChanged", userInfo: nil, repeats: true)
    }
    // 结束动画
    func endAnimating(){
        isAnimating = false
    }
    
    //MARK: -监听事件
    func valueChanged(){
        
        if isAnimating {
            self.progress += 0.05
            
            if self.progress >= 0.95 {
                self.progress = 0.95
            }
        }else {
            if self.progress == 1 {
                timer?.invalidate()
            }else {
                self.progress += 0.05
            }
        }
    }
}
